#!/usr/bin/env python3
# -*- encoding: utf-8 -*-
'''
@author: yuejl
@application:
@contact: lewyuejian@163.com
@file: load_file.py
@time: 2021/8/22 0022 17:00
@desc:
'''

import os
import yaml
from common import BASE_PATH
from configparser import ConfigParser

class ReadConfParser(ConfigParser):
    '''
        重写 configparser 中的 optionxform 函数
        解决 .ini 文件中的 键option 自动转为小写的问题
        '''

    def __init__(self, defaults=None):
        ConfigParser.__init__(self, defaults=defaults)

    def optionxform(self, optionstr: str) -> str:
        return optionstr

def load_yaml_file(filePath):
    with open(filePath, 'r', encoding='utf-8') as f:
        yml_content = yaml.load(f, Loader=yaml.CLoader)
        return yml_content

def load_file(fileName, path=BASE_PATH):
    folder_path = os.listdir(path)
    for file in folder_path:
        # 将目录和文件名拼接
        c_path = os.path.join(path, file)
        # 如果是目录则继续调用
        if os.path.isdir(c_path):
            data = load_file(fileName, c_path)
            if data is not None:
                return data
        elif fileName == file:
            data = load_yaml_file(c_path)
            return data

def load_ini(section, filePath):
    """
    读取ini配置文件
    :param section:
    :param filePath:
    :return: 键值对
    """
    filePath = filePath.replace(r'\/'.replace(os.sep, ''), os.sep)
    conf = ReadConfParser()
    conf.read(filePath, encoding='utf-8')
    load_data = dict(conf.items(section))
    return load_data

if __name__ == '__main__':
    data = load_ini()
    